package com.wutu.nbyjk8.service.impl;


import com.wutu.nbyjk8.domain.CarPassThroughRecord;
import com.wutu.nbyjk8.vo.TimeGroupVO;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

public class CarPassThroughRecordHandler {

    public static List<TimeGroupVO> groupByHour(List<CarPassThroughRecord> records) {
        Map<Integer, TimeGroupVO> hourMap = new HashMap<>();

        for (CarPassThroughRecord record : records) {
            Integer type = null;
            Date time = null;

            if ("1".equals(record.getIotype())) {
                time = record.getItime();
                type = record.getItime().getHours();
            } else if ("2".equals(record.getIotype())) {
                time = record.getOtime();
                type = record.getOtime().getHours();
            }

            if (type != null) {
                TimeGroupVO timeGroup = hourMap.getOrDefault(type, new TimeGroupVO());
                timeGroup.setType(type.toString());

                if ("1".equals(record.getIotype())) {
                    if (timeGroup.getInNum() != null) {
                        timeGroup.setInNum(timeGroup.getInNum() + 1);
                    } else {
                        timeGroup.setInNum(1);
                    }
                } else if ("2".equals(record.getIotype())) {
                    if (timeGroup.getOutNum() != null) {
                        timeGroup.setOutNum(timeGroup.getOutNum() + 1);
                    } else {
                        timeGroup.setOutNum(1);
                    }
                }

                hourMap.put(type, timeGroup);
            }
        }

        // 填充没有数据的小时
        for (int i = 0; i < 24; i++) {
            TimeGroupVO timeGroup = hourMap.getOrDefault(i, new TimeGroupVO());
            timeGroup.setType(String.valueOf(i));
            hourMap.put(i, timeGroup);
        }

        return new ArrayList<>(hourMap.values());
    }


    public static List<TimeGroupVO> groupByDate(List<CarPassThroughRecord> records, String startDateStr, String endDateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, TimeGroupVO> dateMap = new TreeMap<>();
        try {
            Date startDate = sdf.parse(startDateStr);
            Date endDate = sdf.parse(endDateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);

            // 初始化日期范围
            while (!calendar.getTime().after(endDate)) {
                String currentDateStr = new SimpleDateFormat("MM-dd").format(calendar.getTime());
                TimeGroupVO timeGroup = new TimeGroupVO();
                timeGroup.setType(currentDateStr);
                dateMap.put(currentDateStr, timeGroup);
                calendar.add(Calendar.DATE, 1);
            }

            for (CarPassThroughRecord record : records) {
                String type = null;
                Date time = null;

                if ("1".equals(record.getIotype())) {
                    time = record.getItime();
                } else if ("2".equals(record.getIotype())) {
                    time = record.getOtime();
                }

                if (time != null) {


                    String formattedDate = new SimpleDateFormat("MM-dd").format(time);
                    TimeGroupVO timeGroup = dateMap.get(formattedDate);
                    if ("1".equals(record.getIotype())) {
                        if (timeGroup.getInNum() != null) {
                            timeGroup.setInNum(timeGroup.getInNum() + 1);
                        } else {
                            timeGroup.setInNum(1);
                        }
                    } else if ("2".equals(record.getIotype())) {
                        if (timeGroup.getOutNum() != null) {
                            timeGroup.setOutNum(timeGroup.getOutNum() + 1);
                        } else {
                            timeGroup.setOutNum(1);
                        }
                    }
                    dateMap.put(formattedDate, timeGroup);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ArrayList<>(dateMap.values());
    }

    public static List<TimeGroupVO> groupByDay(List<CarPassThroughRecord> records, Date startTime, Date endTime) {
        Map<String, TimeGroupVO> map = new TreeMap<>(); // 自动对 key 进行排序
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

        // Fill missing days
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while (!calendar.getTime().after(endTime)) {
            String day = sdfDay.format(calendar.getTime());
            final TimeGroupVO timeGroupVO = new TimeGroupVO();
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            String weekDay = getWeekDayString(dayOfWeek);
            timeGroupVO.setType(weekDay);
            map.put(day, timeGroupVO);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        for (CarPassThroughRecord record : records) {

            Date date = record.getOtime();
            String day = sdfDay.format(date);
            TimeGroupVO vo = map.get(day);
            if (vo != null) {
                calendar.setTime(date);
                int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                String weekDay = getWeekDayString(dayOfWeek);
                vo.setType(weekDay);
                if (StringUtils.isEmpty(record.getMon())) {
                    continue;
                }
                vo.setMon(vo.getMon().add(new BigDecimal(record.getMon())));
            }
        }

        return new ArrayList<>(map.values());
    }

    public static List<TimeGroupVO> groupByMonth(List<CarPassThroughRecord> records, Date startTime, Date endTime) {
        Map<String, TimeGroupVO> map = new TreeMap<>(); // 自动对 key 进行排序
        SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
        // Fill missing months
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while (!calendar.getTime().after(endTime)) {
            String month = sdfMonth.format(calendar.getTime());
            final TimeGroupVO timeGroupVO = new TimeGroupVO();
            timeGroupVO.setType(month);
            map.put(month, timeGroupVO);
            calendar.add(Calendar.MONTH, 1);
        }
        for (CarPassThroughRecord record : records) {
            String month = sdfMonth.format(record.getOtime());
            TimeGroupVO vo = map.get(month);
            if (vo != null) {
                if (StringUtils.isEmpty(record.getMon())) {
                    continue;
                }
                vo.setMon(vo.getMon().add(new BigDecimal(record.getMon())));
            }
        }
        return new ArrayList<>(map.values());
    }


    private static String getWeekDayString(int dayOfWeek) {
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                return "周日";
            case Calendar.MONDAY:
                return "周一";
            case Calendar.TUESDAY:
                return "周二";
            case Calendar.WEDNESDAY:
                return "周三";
            case Calendar.THURSDAY:
                return "周四";
            case Calendar.FRIDAY:
                return "周五";
            case Calendar.SATURDAY:
                return "周六";
            default:
                return "";
        }
    }
}

